package com.dean.service;

import com.dean.constants.DeanUiConstants;
import com.dean.utils.PropertyUtils;
import io.minio.DownloadObjectArgs;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

public class MinioClietService {
    private static final String MINIO_CFG = DeanUiConstants.INIT_PATH+"minio.properties";
    private String endpoint;
    private String accessKey;
    private String screctKey;
    private String bucket;
    private MinioClient minioClient;

    private MinioClietService(){
        Properties miniocCfg = PropertyUtils.readProperties(MINIO_CFG);
        endpoint = miniocCfg.getProperty("endpoint");
        accessKey = miniocCfg.getProperty("accessKey");
        screctKey = miniocCfg.getProperty("screctKey");
        bucket = miniocCfg.getProperty("bucket");
        minioClient = MinioClient.builder().endpoint(endpoint).credentials(accessKey,screctKey).build();
    }
    private static volatile MinioClietService INSTANCE;
    public static MinioClietService getInstance(){
        if(INSTANCE == null){
            synchronized (MinioClietService.class){
                if(INSTANCE == null){
                    INSTANCE = new MinioClietService();
                }
            }
        }
        return INSTANCE;
    }

    public  MinioClient getMinioClient(){
        return getInstance().minioClient;
    }

    public  String getBucket(){
        return getInstance().bucket;
    }

    /**
     *
     * @param is // 注意有坑
     * @param objectName
     * @throws ServerException
     * @throws InsufficientDataException
     * @throws ErrorResponseException
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws InvalidResponseException
     * @throws XmlParserException
     * @throws InternalException
     */
    @Deprecated
    public void upload(InputStream is, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        upload(is,is.available(),objectName);
    }

    public void upload(InputStream is,long objectSize, String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient client  = getMinioClient();
        client.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName).
                stream(is, objectSize, -1).build());
    }

    public InputStream download(String objectName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient client = getMinioClient();
        InputStream is = client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
        return is;
    }

    public void download(String objectName,String fileName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient client = getMinioClient();
        client.downloadObject(DownloadObjectArgs.builder().bucket(bucket).object(objectName).filename(fileName).build());
    }
}
